package com.situ.library.controller;

import com.situ.library.controller.BaseController;
import com.situ.library.pojo.entity.Card;
import com.situ.library.pojo.query.CardQuery;
import com.situ.library.service.ICardService;
import com.situ.library.service.impl.CardServiceImpl;
import com.situ.library.util.MyBeanUtils;
import com.situ.library.util.PageInfo;
import com.situ.library.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/card")
public class CardController extends BaseController {
    private ICardService cardService = new CardServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "selectAll":
                selectAll(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
        }
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        cardService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        toJSON(resp, Result.ok("更新成功"));
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardController.selectAll");
        List<Card> list = cardService.selectAll();
        toJSON(resp, Result.ok(list));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardServlet.update");
        Card card = MyBeanUtils.copyToBean(req, Card.class);
        cardService.update(card);
        toJSON(resp, Result.ok("更新成功"));
    }



    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardServlet.selectById");
        String id = req.getParameter("id");
        Card card = cardService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(card));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardServlet.add");
        Card card = MyBeanUtils.copyToBean(req, Card.class);
        cardService.add(card);
        toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        cardService.deleteAll(ids);
        toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardServlet.deleteById");
        String id = req.getParameter("id");
        cardService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CardServlet.selectByPage");
        CardQuery cardQuery = MyBeanUtils.copyToBean(req, CardQuery.class);
        System.out.println(cardQuery);
        PageInfo pageInfo = cardService.selectByPage(cardQuery);

        toJSON(resp, pageInfo);
    }

}
